import { Camera, PerspectiveCamera, WebGLRenderer } from "three"
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import TWEEN from "three/examples/jsm/libs/tween.module.js"
import type { MovePoint } from "./interfaces"
// 初始化镜头
export const initCamera = () => {

  const camera = new PerspectiveCamera(100, window.innerWidth / window.innerHeight, 1, 100);
  return camera
}

const moveCam = (cam: Camera, controls: OrbitControls, point: MovePoint, callback: FunctionConstructor | undefined) => {
  const tween = new TWEEN.Tween({
    x1: point.old.P.x,
    y1: point.old.P.y,
    z1: point.old.P.z,
    x2: point.old.T.x,
    y2: point.old.T.y,
    z2: point.old.T.z
  });
  tween.to(
    {
      x1: point.new.P.x,
      y1: point.new.P.y,
      z1: point.new.P.z,
      x2: point.new.T.x,
      y2: point.new.T.y,
      z2: point.new.T.z
    },
    1000
  );
  // 每一帧执行函数 、这个地方就是核心了、每变一帧跟新一次页面元素
  tween.onUpdate((object) => {
    cam.position.set(object.x1, object.y1, object.z1);
    controls.target.x = object.x2;
    controls.target.y = object.y2;
    controls.target.z = object.z2;
    controls.update();
  });

  // 动画完成后的执行函数
  tween.onComplete(() => {
    controls.enabled = true;

    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
    callback && callback();
  });

  tween.easing(TWEEN.Easing.Cubic.InOut);

  // 这个函数必须有、这个是启动函数、不加不能启动
  tween.start();
}
